package com.jf.controller;

import com.alibaba.fastjson.JSON;
import com.jf.bean.Userinfo;
import com.jf.form.LoginForm;
import com.jf.service.RoleService;
import com.jf.service.TokenService;
import com.jf.service.UserinfoService;
import com.jf.service.impl.RoleServiceImpl;
import com.jf.service.impl.TokenServiceImpl;
import com.jf.service.impl.UserinfoServiceImpl;
import com.jf.util.MyConstants;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;
import com.jf.util.ResultJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@WebServlet("/userinfo")
public class UserinfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("param");
        UserinfoService userinfoService = new UserinfoServiceImpl();
        RoleService roleService = new RoleServiceImpl();
        TokenService tokenService = new TokenServiceImpl();

        BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        String result = MyUtils.getRequestJson(in);
        Userinfo userinfo = JSON.parseObject(result,Userinfo.class);

        if(MyUtils.isStringEmpty(param)){
            request.setAttribute("roleList",roleService.getAll());
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-list.jsp").forward(request,response);
        }
        if("list".equals(param)){
            int page = MyUtils.string2Int(request.getParameter("page"),1);
            int limit = MyUtils.string2Int(request.getParameter("limit"),5);
            Userinfo searchForm = new Userinfo();
            searchForm.setUsername(request.getParameter("username"));
            searchForm.setNickname(request.getParameter("nickname"));
            searchForm.setPhone(request.getParameter("phone"));
            searchForm.setRoleId(MyUtils.string2Int(request.getParameter("roleId")));
            searchForm.setStatus(MyUtils.string2Int(request.getParameter("status")));
            PageInfo pageInfo = new PageInfo(page,limit);
            response.getWriter().println(JSON.toJSON(userinfoService.getPage(pageInfo,searchForm)));
        }
        if("delete".equals(param)){
            int id = MyUtils.string2Int(request.getParameter("id"));
            response.getWriter().println(JSON.toJSON(userinfoService.delete(id)));
        }
        if("edit".equals(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-edit.jsp").forward(request,response);
        }
        if("info".equals(param)){
            Integer id = MyUtils.string2Int(request.getParameter("id"));
            if(id == null){// 未传递id则认为需要获取的是当前登录用户的id
                Userinfo loginInfo = tokenService.getUserByToken(request.getHeader("token"));
                id = loginInfo.getId();
            }
            response.getWriter().println(JSON.toJSON(ResultJson.success(userinfoService.getById(id))));
        }
        if("update".equals(param)){
            response.getWriter().println(JSON.toJSON(userinfoService.saveOrUpdate(userinfo)));
        }
        if("pwd".equals(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-password.jsp").forward(request,response);
        }
        if("checkPwd".equals(param)){
            /*获取token*/
            String token = request.getHeader("token");
            /*根据token获取用户，最终目的是为了获取用户id*/
            Userinfo tokenUser = tokenService.getUserByToken(token);
            /*获取请求的密码参数值*/
            String password = request.getParameter("password");
            /*封装查询数据*/
            Userinfo searchInfo = new Userinfo();
            searchInfo.setId(tokenUser.getId());
            searchInfo.setPassword(password);

            response.getWriter().println(JSON.toJSON(userinfoService.checkPassword(searchInfo)));
        }
        if("updatePwd".equals(param)){
            /*获取token*/
            String token = request.getHeader("token");
            /*根据token获取用户，最终目的是为了获取用户id*/
            Userinfo tokenUser = tokenService.getUserByToken(token);
            userinfo.setId(tokenUser.getId());
            response.getWriter().println(JSON.toJSON(userinfoService.saveOrUpdate(userinfo)));
        }
        if("logout".equals(param)){
            request.getSession().removeAttribute(MyConstants.LOGIN_SESSION_KEY);
            response.sendRedirect("login");
        }
        if("del".equals(param)){
            String ids = request.getParameter("ids");
            response.getWriter().println(JSON.toJSON(userinfoService.batchDelete(ids)));
        }
    }
}
